/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Estacionamento.DAO;
/**
 *
 * @author Italo Valença
 */
import Estacionamento.ClassesBasicas.Plano;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class PlanoDAO extends Connect_BD {

    EntityManagerFactory emf;
    EntityManager em;

    public PlanoDAO() {
        emf = Persistence.createEntityManagerFactory("estacionamento_praticodb");
        em = emf.createEntityManager();
    }

    public void CadastrarPlano(Plano plano) throws Exception {
        try {
            em.getTransaction().begin();
            em.merge(plano);
            em.getTransaction().commit();
            emf.close();
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

    public void ExcluirPlano(Plano plano) throws Exception {
        try {
            em.getTransaction().begin();
            /*em.remove(produto);*/
            em.remove(em.getReference(Plano.class, plano.getId()));
            em.getTransaction().commit();
            emf.close();
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());

        }
    }

    public void AlterarPlano(Plano plano) throws Exception {
        try {
            em.getTransaction().begin();
            em.merge(plano);
            em.getTransaction().commit();
            emf.close();
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }

    }

    public List<Plano> ListarPlano() throws Exception {

        try {
            em.getTransaction().begin();
            Query consulta = em.createQuery("select plano from Plano plano");
            List<Plano> plano = consulta.getResultList();
            em.getTransaction().commit();
            emf.close();
            return plano;

        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }
}
